home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / sendmail.8.8.4.tar.gz / sendmail.8.8.4.tar / sendmail-8.8.4 / mailstats / mailstats.c < prev    next >
C/C++ Source or Header  |  1996-09-25  |  6KB  |  241 lines

  1. /*
  2.  * Copyright (c) 1983 Eric P. Allman
  3.  * Copyright (c) 1988, 1993
  4.  *    The Regents of the University of California.  All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *    This product includes software developed by the University of
  17.  *    California, Berkeley and its contributors.
  18.  * 4. Neither the name of the University nor the names of its contributors
  19.  *    may be used to endorse or promote products derived from this software
  20.  *    without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  *
  34.  */
  35.  
  36. #ifndef lint
  37. static char copyright[] =
  38. "@(#) Copyright (c) 1988, 1993\n\
  39.     The Regents of the University of California.  All rights reserved.\n";
  40. #endif /* not lint */
  41.  
  42. #ifndef lint
  43. static char sccsid[] = "@(#)mailstats.c    8.8 (Berkeley) 9/25/96";
  44. #endif /* not lint */
  45.  
  46. #define NOT_SENDMAIL
  47. #include <sendmail.h>
  48. #include <mailstats.h>
  49. #include <pathnames.h>
  50.  
  51. #define MNAMELEN    20    /* max length of mailer name */
  52.  
  53. main(argc, argv)
  54.     int argc;
  55.     char **argv;
  56. {
  57.     extern char *optarg;
  58.     extern int optind;
  59.     struct statistics stat;
  60.     register int i;
  61.     int mno;
  62.     int ch, fd;
  63.     char *sfile;
  64.     char *cfile;
  65.     FILE *cfp;
  66.     bool mnames;
  67.     long frmsgs = 0, frbytes = 0, tomsgs = 0, tobytes = 0;
  68.     char mtable[MAXMAILERS][MNAMELEN+1];
  69.     char sfilebuf[100];
  70.     char buf[MAXLINE];
  71.     extern char *ctime();
  72.  
  73.     cfile = _PATH_SENDMAILCF;
  74.     sfile = NULL;
  75.     mnames = TRUE;
  76.     while ((ch = getopt(argc, argv, "C:f:o")) != EOF)
  77.     {
  78.         switch (ch)
  79.         {
  80.           case 'C':
  81.             cfile = optarg;
  82.             break;
  83.  
  84.           case 'f':
  85.             sfile = optarg;
  86.             break;
  87.  
  88.           case 'o':
  89.             mnames = FALSE;
  90.             break;
  91.  
  92.           case '?':
  93.           default:
  94.   usage:
  95.             fputs("usage: mailstats [-C cffile] [-f stfile] -o\n",
  96.                 stderr);
  97.             exit(EX_USAGE);
  98.         }
  99.     }
  100.     argc -= optind;
  101.     argv += optind;
  102.  
  103.     if (argc != 0)
  104.         goto usage;
  105.  
  106.     if ((cfp = fopen(cfile, "r")) == NULL)
  107.     {
  108.         fprintf(stderr, "mailstats: ");
  109.         perror(cfile);
  110.         exit(EX_NOINPUT);
  111.     }
  112.  
  113.     mno = 0;
  114.     (void) strcpy(mtable[mno++], "prog");
  115.     (void) strcpy(mtable[mno++], "*file*");
  116.     (void) strcpy(mtable[mno++], "*include*");
  117.  
  118.     while (fgets(buf, sizeof(buf), cfp) != NULL)
  119.     {
  120.         register char *b;
  121.         char *s;
  122.         register char *m;
  123.  
  124.         b = buf;
  125.         switch (*b++)
  126.         {
  127.           case 'M':        /* mailer definition */
  128.             break;
  129.  
  130.           case 'O':        /* option -- see if .st file */
  131.             if (strncasecmp(b, " StatusFile", 11) == 0 &&
  132.                 !isalnum(b[11]))
  133.             {
  134.                 /* new form -- find value */
  135.                 b = strchr(b, '=');
  136.                 if (b == NULL)
  137.                     continue;
  138.                 while (isspace(*++b))
  139.                     continue;
  140.             }
  141.             else if (*b++ != 'S')
  142.             {
  143.                 /* something else boring */
  144.                 continue;
  145.             }
  146.  
  147.             /* this is the S or StatusFile option -- save it */
  148.             strcpy(sfilebuf, b);
  149.             b = strchr(sfilebuf, '#');
  150.             if (b == NULL)
  151.                 b = strchr(sfilebuf, '\n');
  152.             if (b == NULL)
  153.                 b = &sfilebuf[strlen(sfilebuf)];
  154.             while (isspace(*--b))
  155.                 continue;
  156.             *++b = '\0';
  157.             if (sfile == NULL)
  158.                 sfile = sfilebuf;
  159.  
  160.           default:
  161.             continue;
  162.         }
  163.  
  164.         if (mno >= MAXMAILERS)
  165.         {
  166.             fprintf(stderr,
  167.                 "Too many mailers defined, %d max.\n",
  168.                 MAXMAILERS);
  169.             exit(EX_SOFTWARE);
  170.         }
  171.         m = mtable[mno];
  172.         s = m + MNAMELEN;        /* is [MNAMELEN+1] */
  173.         while (*b != ',' && !isspace(*b) && *b != '\0' && m < s)
  174.             *m++ = *b++;
  175.         *m = '\0';
  176.         for (i = 0; i < mno; i++)
  177.         {
  178.             if (strcmp(mtable[i], mtable[mno]) == 0)
  179.                 break;
  180.         }
  181.         if (i == mno)
  182.             mno++;
  183.     }
  184.     (void) fclose(cfp);
  185.     for (; mno < MAXMAILERS; mno++)
  186.         mtable[mno][0]='\0';
  187.  
  188.     if (sfile == NULL)
  189.     {
  190.         fprintf(stderr, "mailstats: no statistics file located\n");
  191.         exit (EX_OSFILE);
  192.     }
  193.  
  194.     if ((fd = open(sfile, O_RDONLY)) < 0 ||
  195.         (i = read(fd, &stat, sizeof stat)) < 0)
  196.     {
  197.         fputs("mailstats: ", stderr);
  198.         perror(sfile);
  199.         exit(EX_NOINPUT);
  200.     }
  201.     if (i == 0)
  202.     {
  203.         sleep(1);
  204.         i = read(fd, &stat, sizeof stat);
  205.         if (i == 0)
  206.         {
  207.             bzero((ARBPTR_T) &stat, sizeof stat);
  208.             (void) time(&stat.stat_itime);
  209.         }
  210.     }
  211.     else if (i != sizeof stat || stat.stat_size != sizeof(stat))
  212.     {
  213.         fputs("mailstats: file size changed.\n", stderr);
  214.         exit(EX_OSERR);
  215.     }
  216.  
  217.     printf("Statistics from %s", ctime(&stat.stat_itime));
  218.     printf(" M msgsfr bytes_from  msgsto   bytes_to%s\n",
  219.         mnames ? "  Mailer" : "");
  220.     for (i = 0; i < MAXMAILERS; i++)
  221.     {
  222.         if (stat.stat_nf[i] || stat.stat_nt[i])
  223.         {
  224.             printf("%2d %6ld %10ldK %6ld %10ldK", i,
  225.                 stat.stat_nf[i], stat.stat_bf[i],
  226.                 stat.stat_nt[i], stat.stat_bt[i]);
  227.             if (mnames)
  228.                 printf("  %s", mtable[i]);
  229.             printf("\n");
  230.             frmsgs += stat.stat_nf[i];
  231.             frbytes += stat.stat_bf[i];
  232.             tomsgs += stat.stat_nt[i];
  233.             tobytes += stat.stat_bt[i];
  234.         }
  235.     }
  236.     printf("========================================\n");
  237.     printf(" T %6ld %10ldK %6ld %10ldK\n",
  238.         frmsgs, frbytes, tomsgs, tobytes);
  239.     exit(EX_OK);
  240. }
  241.